﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class LiuyansController : AdminBaseController
    {
        liuyanCore core = new liuyanCore();
        public ActionResult GetOne(int id)
        {
            if (!HasQx("liuyan"))
            {
                return Error("没有权限！");
            }
            return Success(core.GetOne(id));
        }
        public ActionResult GetList(pageinfo pageinfo)
        {
            if (!HasQx("liuyan"))
            {
                return Error("没有权限！");
            }
            List<object> argsList = new List<object>();
            string where = string.Empty;
            //where += " and endtime>=now()";
            if (!string.IsNullOrEmpty(pageinfo.key))
            {
                where += " and title like @" + argsList.Count + " or ID like @"+argsList.Count + " or username like @" + argsList.Count + " or tel like @" + argsList.Count + " or email like @" + argsList.Count; ;
                argsList.Add($"%{pageinfo.key}%");
            }
            var list = core.GetPageList(pageinfo.page, pageinfo.pagesize, where, pageinfo.order + " " + pageinfo.esc, argsList.ToArray());
            return Success(list);
        }
        [HttpPost]
        public ActionResult Save(liuyanEntity liuyan)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            try
            {

                if (liuyan.ID == 0)
                {
                    return Error("后台不能添加留言");
                }
                else
                {
                    liuyan.retime = DateTime.Now;
                    liuyan.zt = true;
                }

                var model = core.Save(liuyan);
                Log(curmanage.username + "回复留言完成" + model.ToJson());
                return Success(model);
            }
            catch (Exception e1)
            {
                LoggerManager.Debug(e1.Message, "XinxiController");
                return Error();
            }
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (core.Del(id))
                {
                    Log(curmanage.username + "删除留言ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<liuyanEntity> modelList)
        {
            if (!HasQx("article"))
            {
                return Error("没有权限！");
            }
            string ids = string.Empty;
            foreach (var liuyanEntity in modelList)
            {
                core.Del(liuyanEntity);
                ids += liuyanEntity.ID + ",";
            }
            Log(curmanage.username + "删除留言ID in (" + ids + ")");
            return Success("删除成功！");
        }
    }
}
